/**
 * @(#) SecurityUtil.java
 */
package com.airclick.merchant.utils;

import java.security.MessageDigest;


/**
 * 本类是本安全包的主类，负责提供公开的静态的方法供其它模块调用，采用BouncyCastleProvider 
 * 功能如下：
 * @author caiwangjue
 */
public class SecurityUtil {

	private SecurityUtil() {
	}

	/**
	 * 对数据做MD5摘要
	 * 
	 * @param aData
	 *            源数据
	 * @return 摘要结果
	 * @throws SecurityException
	 * @author nilomiao
	 * @since 2009-11-27
	 */
	public static String md5Hex(String aData,boolean upper) throws SecurityException {
		System.out.println("MD5Hex::"+aData);
		String resultString = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = bytes2HexString(md.digest(aData.getBytes("UTF-8")));
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("MD5运算失败");
		}
		System.out.println("MD5Hex......"+resultString);
		if(upper){
			return resultString.toUpperCase();
		}else{
			return resultString.toLowerCase();
		}
		
	}
	
	public static String bytes2HexString(byte[] b) {
		StringBuffer ret = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret.append(hex.toUpperCase());
		}
		return ret.toString();
	}

}
